package com.yougou.ui.home;

import android.content.Intent;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.yougou.R;
import com.yougou.base.BaseFragment;
import com.yougou.ui.home.adapter.BannerAdapter;
import com.yougou.ui.home.adapter.FloorAdapter;
import com.yougou.ui.search.SearchActivity;
import com.yougou.utils.ToastUtils;

public class HomeFragment extends BaseFragment {
    
    private ViewPager2 mBannerViewPager;
    private RecyclerView mFloorRecyclerView;
    private HomeViewModel mViewModel;
    private BannerAdapter mBannerAdapter;
    private FloorAdapter mFloorAdapter;
    
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
    
    @Override
    protected void initView(View view) {
        mBannerViewPager = view.findViewById(R.id.viewpager_banner);
        mFloorRecyclerView = view.findViewById(R.id.recyclerview_floor);
        
        // 初始化轮播图
        mBannerAdapter = new BannerAdapter();
        mBannerViewPager.setAdapter(mBannerAdapter);
        
        // 设置轮播图点击事件
        mBannerAdapter.setOnBannerClickListener(goodsId -> {
            Intent intent = new Intent(requireContext(), com.yougou.ui.product.ProductDetailActivity.class);
            intent.putExtra(com.yougou.ui.product.ProductDetailActivity.EXTRA_GOODS_ID, goodsId);
            startActivity(intent);
        });
        
        // 初始化楼层列表
        mFloorRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mFloorAdapter = new FloorAdapter();
        mFloorRecyclerView.setAdapter(mFloorAdapter);
        
        // 搜索框点击事件
        view.findViewById(R.id.layout_search).setOnClickListener(v -> {
            startActivity(new Intent(getContext(), SearchActivity.class));
        });
    }
    
    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        
        // 观察轮播图数据
        mViewModel.getSwiperList().observe(getViewLifecycleOwner(), swipers -> {
            if (swipers != null && !swipers.isEmpty()) {
                mBannerAdapter.setData(swipers);
            }
        });
        
        // 观察楼层数据
        mViewModel.getFloorList().observe(getViewLifecycleOwner(), floors -> {
            if (floors != null && !floors.isEmpty()) {
                mFloorAdapter.setData(floors);
            }
        });
        
        // 观察错误信息
        mViewModel.getError().observe(getViewLifecycleOwner(), error -> {
            if (error != null) {
                ToastUtils.show(requireContext(), error);
            }
        });
        
        // 设置楼层商品点击事件
        mFloorAdapter.setOnProductClickListener(query -> {
            // 跳转到搜索结果页
            Intent intent = new Intent(requireContext(), SearchActivity.class);
            intent.putExtra(SearchActivity.EXTRA_SEARCH_QUERY, query);
            startActivity(intent);
        });
        
        // 加载数据
        mViewModel.loadHomeData();
    }
}

